<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Controller;
use App\Repositories\Manage\RoleRepo;
use App\Repositories\Manage\MenuRepo;

class BaseController extends Controller
{
    protected $noAccessMsg = '你没有权限进行该操作，请与管理员联系';

    protected function actionAuthorize($menuId, $action)
    {
        $uid = session('manage_user')->uid;
        $roleRepo = new roleRepo();
        $menuRepo = new MenuRepo();
        $roleId = $roleRepo->getRoleId('user_id = ?', [$uid]);
        $data = $roleRepo->getRoleMenu('role_id = ? and menu_id = ?', [$roleId, $menuId]);
        if (empty($data)) {
            abort(403, $this->noAccessMsg);
        } else {
            $power = $data->power;
            $menuModule = $menuRepo->getModule('menu_id = ? and action = ?', [$menuId, $action]);
            $value = $menuModule->value;
            $row = (int)$power & (int)$value;
            if ($row == 0) {
                abort(403, $this->noAccessMsg);
            }
        }
    }

   protected function ajaxResponse($errorCode,$msg){
      return response()->json(['error' => $errorCode, 'message' => $msg]);
   }
}
